{%TEMPLATE->struct/head%}
{%TEMPLATE->struct/menu%}
<h2>API Management</h2>
{%TEMPLATE->struct/messages%}
<h3>Api Access Groups</h3>
<div class="card">
  <div class="table-responsive">
    <table class="table table-bordered">
      <tr>
        <th colspan="5" class="text-center">
          <a href="api.php?new=true"><button class="btn btn-success">Create New Api Group</button></a>
        </th>
      </tr>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th># of Users</th>
        <th>Action</th>
      </tr>
      {{FOREACH group;[[groups]]}}
        <tr>
          <td>[[group.getId()]]</td>
          <td>[[htmlentities([[group.getName()]], ENT_QUOTES, "UTF-8")]]</td>
          <td>[[apis.getVal([[group.getId()]])]]</td>
          <td>
            <form class="float-right mx-1" action="api.php" method="POST" onSubmit="if (!confirm('Really delete api group [[htmlentities([[group.getName()]], ENT_QUOTES, 'UTF-8')]]?')) return false;">
              <input type="hidden" name="action" value="[[$DApiAction::DELETE_GROUP]]">
              <input type="hidden" name="csrf" value="[[csrf]]">
              <input type="hidden" name="groupId" value="[[group.getId()]]">
              <button type="submit" class='btn btn-danger' value="Delete" {{IF [[apis.getVal([[group.getId()]])]] > 0}} disabled{{ENDIF}}><i class="fas fa-trash" aria-hidden="true"></i></button>
            </form>
            <div class="float-right mx-1">
              <a class='btn btn-primary' href="api.php?id=[[group.getId()]]" data-toggle="tooltip" data-placement="top" title="Edit"><i class="fas fa-edit" aria-hidden="true"></i></a>
            </div>
          </td>
        </tr>
      {{ENDFOREACH}}
    </table>
  </div>
</div>
<h3>Api Keys</h3>
<div class="card">
  <div class="table-responsive">
    <table class="table table-bordered">
      <tr>
        <th colspan="5" class="text-center">
          <a href="api.php?newkey=true" class="btn btn-success">Create New Api Key</a>
        </th>
      </tr>
      <tr>
        <th>ID</th>
        <th>User</th>
        <th>Key</th>
        <th>Action</th>
      </tr>
      {{FOREACH key;[[keys]]}}
        <tr>
          <td>[[key.getId()]]</td>
          <td>[[Util::getUsernameById([[key.getUserId()]])]]</td>
          <td><pre>[[key.getAccessKey()]]</pre></td>
          <td>
            <form class="float-right mx-1" action="api.php" method="POST" onSubmit="if (!confirm('Really delete this api key?')) return false;">
              <input type="hidden" name="action" value="[[$DApiAction::DELETE_KEY]]">
              <input type="hidden" name="csrf" value="[[csrf]]">
              <input type="hidden" name="keyId" value="[[key.getId()]]">
              <button type="submit" class='btn btn-danger' value="Delete" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fas fa-trash" aria-hidden="true"></i></button>
            </form>
            <div class="float-right mx-1">
              <a class='btn btn-primary' href="api.php?keyId=[[key.getId()]]" data-toggle="tooltip" data-placement="top" title="Edit"><i class="fas fa-edit" aria-hidden="true"></i></a>
            </div>
          </td>
        </tr>
      {{ENDFOREACH}}
    </table>
  </div>
</div>
{%TEMPLATE->struct/foot%}
